package com.hctforgreen.greenservice;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.hctforgreen.greenservice.ctr.HctController;
import com.hctforgreen.greenservice.model.VideoTypeEntity;
import com.hctforgreen.greenservice.model.VideoTypeEntity.VideoDetailEntity;
import com.hctforgreen.greenservice.ui.adapter.VideoDetailAdapter;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.HctResult;
import com.hctforgreen.greenservice.utils.LoginResultStoreUtil;

/**
 * 视频分类更多详情页面
 * 
 * @author denghb
 * 
 */
public class VideoDetailsActivity extends ParentActivity {

	private int pageNum = 1;// 当前页 初始值为1
	private VideoDetailAdapter mAdapter;
	private List<VideoDetailEntity> data = new ArrayList<VideoDetailEntity>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_type_detail);
		initSkinLayout();
		initTitleButtonBar();
		final String typeId = getIntent().getStringExtra("typeId");
		if (typeId != null) {
			initDataAndWindowByTypeId(typeId);
		} else {
			final View convertView = findViewById(R.id.lyt_default_content);
			final View loadView = convertView
					.findViewById(R.id.lyt_default_list_load);
			loadView.setVisibility(View.GONE);
		}

		final String searchName = getIntent().getStringExtra("searchName");
		if (searchName != null && searchName.trim().length() > 0) {
			initDataAndWindowBySearchName(searchName);
		}
	}

	private void initDataAndWindowBySearchName(final String searchName) {

		final View convertView = findViewById(R.id.lyt_default_content);
		final PullToRefreshGridView gridView = (PullToRefreshGridView) convertView
				.findViewById(R.id.pull_refresh_grid);
		final View loadView = convertView
				.findViewById(R.id.lyt_default_list_load);
		mAdapter = new VideoDetailAdapter(VideoDetailsActivity.this, data,
				gridView.getRefreshableView());
		gridView.getRefreshableView().setAdapter(mAdapter);
		gridView.setOnRefreshListener(new OnRefreshListener2<GridView>() {

			@Override
			public void onPullDownToRefresh(
					PullToRefreshBase<GridView> refreshView) {// 下拉刷新
				pageNum = 1;
				initData(gridView, loadView, null, searchName, false);
			}

			@Override
			public void onPullUpToRefresh(
					PullToRefreshBase<GridView> refreshView) {// 上拉加载更多
				pageNum++;
				initData(gridView, loadView, null, searchName, false);
			}

		});

		initData(gridView, loadView, null, searchName, true);

	}

	private void initDataAndWindowByTypeId(final String typeId) {

		final View convertView = findViewById(R.id.lyt_default_content);
		final PullToRefreshGridView gridView = (PullToRefreshGridView) convertView
				.findViewById(R.id.pull_refresh_grid);
		final View loadView = convertView
				.findViewById(R.id.lyt_default_list_load);
		mAdapter = new VideoDetailAdapter(VideoDetailsActivity.this, data,
				gridView.getRefreshableView());
		gridView.getRefreshableView().setAdapter(mAdapter);
		gridView.setOnRefreshListener(new OnRefreshListener2<GridView>() {

			@Override
			public void onPullDownToRefresh(
					PullToRefreshBase<GridView> refreshView) {// 下拉刷新
				pageNum = 1;
				initData(gridView, loadView, typeId, null, false);
			}

			@Override
			public void onPullUpToRefresh(
					PullToRefreshBase<GridView> refreshView) {// 上拉加载更多
				pageNum++;
				initData(gridView, loadView, typeId, null, false);
			}

		});

		initData(gridView, loadView, typeId, null, true);

	}

	private void initData(final PullToRefreshGridView gridView,
			final View loadView, final String typeId, final String searchName,
			final boolean showLoadView) {
		final Handler handler = new Handler() {
			@Override
			public void dispatchMessage(Message msg) {
				switch (msg.what) {

				case HctConstants.STATUS_FAILD:
					if (pageNum == 1) {
						data.clear();
						mAdapter.notifyDataSetChanged();
					}
					gridView.onRefreshComplete();
					break;
				case HctConstants.STATUS_SUCCESS:
					HctResult hctResult = (HctResult) msg.obj;
					if (hctResult != null && hctResult.data != null) {
						List<VideoTypeEntity> netData = (List<VideoTypeEntity>) hctResult.data;
						if (netData != null && netData.size() > 0
								&& netData.get(0).entities != null
								&& !netData.get(0).entities.isEmpty()) {
							if (pageNum > 1) {// 加载更多
								data.addAll(netData.get(0).entities);
							} else {// 首页数据
								data.clear();
								data.addAll(netData.get(0).entities);
							}
						}
						mAdapter.notifyDataSetChanged();
					}
					gridView.onRefreshComplete();
					break;

				default:
					break;
				}
				loadView.setVisibility(View.GONE);
				gridView.setVisibility(View.VISIBLE);
				super.dispatchMessage(msg);
			}
		};

		if (showLoadView) {
			loadView.setVisibility(View.VISIBLE);
		}
		new Thread() {
			public void run() {
				HctController controller = new HctController(
						VideoDetailsActivity.this);
				try {
					HctResult hctResult = null;
					if (typeId != null) {

						hctResult = controller.getAllVideosByTypeId(typeId,
								LoginResultStoreUtil
										.get(getApplicationContext()).personId,
								pageNum);
					} else if (searchName != null
							&& searchName.trim().length() > 0) {
						hctResult = controller
								.searchVideoByCriteria(
										searchName,
										LoginResultStoreUtil
												.get(VideoDetailsActivity.this).personId,
										pageNum);
					}
					handler.sendMessage(handler.obtainMessage(
							HctConstants.STATUS_SUCCESS, hctResult));
				} catch (Exception e) {
					handler.sendEmptyMessage(HctConstants.STATUS_FAILD);
					e.printStackTrace();
				}

			};
		}.start();

	}

	/**
	 * 初始化标题栏
	 */
	private void initTitleButtonBar() {
		final String typeName = getIntent().getStringExtra("typeName");
		final TextView titleTv = (TextView) findViewById(R.id.tv_title);
		titleTv.setText(typeName + getString(R.string.type_videos_title_tag));

		final ImageButton backBtn = (ImageButton) findViewById(R.id.btn_back);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

	}

}
